package com.my.study.controller;

import com.my.study.common.TopicConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 主题Controller
 *
 * @author: yidujun
 * @create: 2021/07/22 10:26
 */
@RestController
@RequestMapping("/topic")
public class TopicController {

    @Resource
    TopicConfig topicConfig;

    @RequestMapping("/create")
    public Object create(@RequestParam(value = "name", required = false) String name) {
        String topic = topicConfig.createTopic(name);
        return buildSuccess(topic);
    }

    @RequestMapping("/find")
    public Object find(@RequestParam(value = "name", required = false) String name) {
        String topic = topicConfig.selectTopic(name);
        return buildSuccess(topic);
    }

    @RequestMapping("/delete")
    public Object delete(@RequestParam(value = "name", required = false) String name) {
        String topic = topicConfig.deleteTopic(name);
        return buildSuccess(topic);
    }

    private Map<String, Object> buildSuccess(Object data) {
        Map<String, Object> result = new LinkedHashMap<>(2);
        result.put("code", "success");
        result.put("data", data);
        return result;
    }

    private Map<String, Object> buildFail(Object data) {
        Map<String, Object> result = new LinkedHashMap<>(2);
        result.put("code", "error");
        result.put("data", data);
        return result;
    }
}
